/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-04-02
 * Time: 19:49
 */
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode root;

    /**
     *寻找key元素
     * @param key
     * @return
     */
    public boolean search(int key) {
        TreeNode cur = root;
        while(cur != null) {
            //1.判断根结点
            if(cur.val == key) {
                return true;
            } else if(cur.val > key) {
                //2.根结点大于key
                cur = cur.left;
            } else {
                //3.根结点小于key
                    cur = cur.right;
                }
            }
        return false;
        }
    /**
     * 插入元素
      */
    public void insert(int val) {
        //1.root == null
        if(root == null) {
          root = new TreeNode(val);
            return;
        }
            //2.root != null
            TreeNode cur = root;
            TreeNode parent = null;
            while(cur != null) {
                if(cur.val < val) {
                    //1.根结点小于key
                    parent = cur;
                    cur = cur.right;
                } else {
                    //2.根结点大于key
                    parent = cur;
                    cur = cur.left;
                }
            }
            //说明cur为空，元素插入parent结点的其中两侧
            TreeNode node =  new TreeNode(val);
            if(parent.val < val) {
                parent.right = node;
            } else {
                parent.left = node;
            }
        }

    public boolean remove(int val) {
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else if(cur.val >val){
                parent = cur;
                cur = cur.left;
            } else {
                return removeNode(parent,cur);
            }
        }
        return false;
    }
    private boolean removeNode(TreeNode parent,TreeNode cur) {
        //1.cur.left == null
        if(cur.left == null) {
            if(cur == root) {
               cur.right = root;
            } else {
                if(cur == parent.right) {
                    parent.right = cur.right;
                }
                if(cur == parent.left) {
                    parent.left = cur.right;
                }
            }
        }
        //2.cur.right == null
        if(cur.right == null) {
            if(cur == root) {
                cur.left = root;
            } else {
                if(cur == parent.left) {
                    parent.left = cur.left;
                }
                if(cur == parent.right) {
                    parent.right = cur.left;
                }
            }
        }
        //3.cur.left !=null && cur.right != null
        TreeNode child = cur.right;
        TreeNode p = null;//p为child的父亲结点
        while(child.left != null) {
            p = child;
            child = child.left;
        }
        //找到最小值，将最小值赋值给cur
        cur.val = child.val;

        //最小值的父亲结点的左节点是否等于最小值结点
        if(p.left == child) {
            p.left = child.right;
        } else {
            p.right = child.right;
        }

        return true;
    }

}
